package com.example.rocketmq.order;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * 顺序消费
 */
public class OrderConsumer {
    public static void main(String[] args) throws Exception {
        // 创建消费者，指定消费者组名，通过broker推送来消费
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("order_consumer");
        // 指定nameserver地址
        consumer.setNamesrvAddr("192.168.228.101:9876");
        // 订阅topic 和 tag
        consumer.subscribe("order_topic", "*");
        
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext context) {
                for (MessageExt m : list) {
                    String string = new String(m.getBody());
                    System.out.println(Thread.currentThread().getName() + " " + string);
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });

        consumer.start();
    }
}



